home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / libibert / vasprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  3.4 KB  |  155 lines

  1. /* Like vsprintf but provides a pointer to malloc'd storage, which must
  2.    be freed by the caller.
  3.    Copyright (C) 1994 Free Software Foundation, Inc.
  4.  
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10.  
  11. Libiberty is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with libiberty; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <ansidecl.h>
  24. #ifdef __STDC__
  25. #include <stdarg.h>
  26. #else
  27. #include <varargs.h>
  28. #endif
  29.  
  30. #ifdef TEST
  31. int global_total_width;
  32. #endif
  33.  
  34. unsigned long strtoul ();
  35. char *malloc ();
  36.  
  37. int
  38. vasprintf (result, format, args)
  39.      char **result;
  40.      const char *format;
  41.      va_list args;
  42. {
  43.   const char *p = format;
  44.   /* Add one to make sure that it is never zero, which might cause malloc
  45.      to return NULL.  */
  46.   int total_width = strlen (format) + 1;
  47.   va_list ap = args;
  48.  
  49.   while (*p != '\0')
  50.     {
  51.       if (*p++ == '%')
  52.     {
  53.       while (strchr ("-+ #0", *p))
  54.         ++p;
  55.       if (*p == '*')
  56.         {
  57.           ++p;
  58.           total_width += abs (va_arg (ap, int));
  59.         }
  60.       else
  61.         total_width += strtoul (p, &p, 10);
  62.       if (*p == '.')
  63.         {
  64.           ++p;
  65.           if (*p == '*')
  66.         {
  67.           ++p;
  68.           total_width += abs (va_arg (ap, int));
  69.         }
  70.           else
  71.           total_width += strtoul (p, &p, 10);
  72.         }
  73.       while (strchr ("hlL", *p))
  74.         ++p;
  75.       /* Should be big enough for any format specifier except %s.  */
  76.       total_width += 30;
  77.       switch (*p)
  78.         {
  79.         case 'd':
  80.         case 'i':
  81.         case 'o':
  82.         case 'u':
  83.         case 'x':
  84.         case 'X':
  85.         case 'c':
  86.           (void) va_arg (ap, int);
  87.           break;
  88.         case 'f':
  89.         case 'e':
  90.         case 'E':
  91.         case 'g':
  92.         case 'G':
  93.           (void) va_arg (ap, double);
  94.           break;
  95.         case 's':
  96.           total_width += strlen (va_arg (ap, char *));
  97.           break;
  98.         case 'p':
  99.         case 'n':
  100.           (void) va_arg (ap, char *);
  101.           break;
  102.         }
  103.     }
  104.     }
  105. #ifdef TEST
  106.   global_total_width = total_width;
  107. #endif
  108.   *result = malloc (total_width);
  109.   if (*result != NULL)
  110.     return vsprintf (*result, format, args);
  111.   else
  112.     return 0;
  113. }
  114.  
  115. #ifdef TEST
  116. void
  117. checkit
  118. #ifdef __STDC__
  119.      (const char* format, ...)
  120. #else
  121.      (va_alist)
  122.      va_dcl
  123. #endif
  124. {
  125.   va_list args;
  126.   char *result;
  127.  
  128. #ifdef __STDC__
  129.   va_start (args, format);
  130. #else
  131.   char *format;
  132.   va_start (args);
  133.   format = va_arg (args, char *);
  134. #endif
  135.   vasprintf (&result, format, args);
  136.   if (strlen (result) < global_total_width)
  137.     printf ("PASS: ");
  138.   else
  139.     printf ("FAIL: ");
  140.   printf ("%d %s\n", global_total_width, result);
  141. }
  142.  
  143. int
  144. main ()
  145. {
  146.   checkit ("%d", 0x12345678);
  147.   checkit ("%200d", 5);
  148.   checkit ("%.300d", 6);
  149.   checkit ("%100.150d", 7);
  150.   checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
  151. 777777777777777777333333333333366666666666622222222222777777777777733333");
  152.   checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
  153. }
  154. #endif /* TEST */
  155.